
import redis.clients.jedis.Jedis;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * Created by luoqifei on 17-4-17.
 */
public class Test2 {
    private Jedis jedis;
    private static String fieldSample = "23.7612%s%s,";
    private static long timestamp=1493104167000L;
    public static int counter = 0;
    public Test2(String redisIp,int port){
        jedis = new Jedis(redisIp,port,99999999);
    }
    public static void flush(){
        Test2 test = new Test2("127.0.0.1",2999);
        test.flushDb();
    }
    public void flushDb(){
        jedis.flushDB();
    }

    private long getTimeMinute(long unixTime){
        DateFormat df = new SimpleDateFormat("yyyyMMddHHmm");
        Date date = new Date(unixTime);
        String dateTime = df.format(date);
        try {
            return df.parse(dateTime).getTime()/1000;
        } catch (ParseException e) {
            e.printStackTrace();
            return 0;
        }
    }
    public boolean setRecordWithTimeSlots(long times, String[] fName,String[] fValues,String fgId,String compoundId){
        //asset=fgId:compoundId
        long time = getTimeMinute(times);
        jedis.zadd("idx::"+fgId+":"+compoundId+"::"+time,times,""+times);
        counter = counter+1;
        for(int i=0;i<fName.length;i++){
            jedis.hset("dat::"+compoundId+"::"+fName[i]+"::"+time,fgId+":"+times,fValues[i]);
            counter = counter+1;
        }
        return true;
    }
    public boolean setRecord(long times, String[] fName,String[] fValues,String fgId,String compoundId){
        //asset=fgId:compoundId
        jedis.zadd("idx::"+fgId+":"+compoundId,times,""+times);
        counter++;
        for(int i=0;i<fName.length;i++){
            jedis.hset("dat::"+compoundId+"::"+fName[i],fgId+":"+times,fValues[i]);
            counter++;
        }
        return true;
    }
    public static void main(String args[]){
        //writeRecords(60);
        flush();
        //removeData();
    }
    public static void writeRecords(int secs){
        Test2 test = new Test2("127.0.0.1",2999);
        int fgNum = 1;
        int assetNum = 60;
        int fieldNum = 47;
        long seconds = secs;
        long writeSpeed = 200;
        String[] fgIds = new String[fgNum];
        String[] compoundIds = new String[assetNum];
        String[] filedNames = new String[fieldNum];
        String[] filedValues = new String[fieldNum];
        for(int b=0;b<fgNum;b++){
            fgIds[b]="fieldGroupId_"+b;
        }
        for(int b=0;b<assetNum;b++){
            compoundIds[b]="compoundId_"+b;
        }
        for(int i=0;i<fieldNum;i++){
            filedNames[i] = "fieldName_"+i;
            filedValues[i] = String.format(fieldSample,String.valueOf(i+1),String.valueOf(i+1));
        }
        long time=System.currentTimeMillis();
        for(int b=0;b<fgNum;b++){
            for(int k=0;k<assetNum;k++){
                for(int j=0;j<seconds*writeSpeed;j++){
                    test.setRecordWithTimeSlots(timestamp+j*5,filedNames,filedValues,fgIds[b],compoundIds[k]);
                    //test.setRecord(timestamp+j*5,filedNames,filedValues,fgIds[b],compoundIds[k]);
                }
            }
        }
        System.out.println("had execute redis write "+counter+" .");
        System.out.println((System.currentTimeMillis()-time)/1000+" seconds");
    }
}
